package org.jeecg.qis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.basic.vo.MesOptionVO;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.qis.entity.MesProcDataCfg;
import org.jeecg.qis.entity.QisRuleCfg;
import org.jeecg.qis.mapper.MesProcDataCfgMapper;
import org.jeecg.qis.mapper.QisRuleCfgMapper;
import org.jeecg.qis.service.IMesProcDataCfgService;
import org.jeecg.qis.service.IQisRuleCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 设备检验参数配置
 * @Author: LiCY
 * @Date:   2023-06-19
 * @Version: V1.0
 */
@Service
public class QisRuleCfgServiceImpl extends ServiceImpl<QisRuleCfgMapper, QisRuleCfg> implements IQisRuleCfgService {


    @Override
    public List<MesOptionVO> getRuleCfgSelect() {
        return this.baseMapper.getRuleCfgSelect();
    }

    @Override
    public List<QisRuleCfg> getRuleCfgList() {

        return null;
    }

    @Override
    public boolean validateRuleRange(QisRuleCfg qisRuleCfg) {
        QueryWrapper<QisRuleCfg> wrapper = new QueryWrapper<>();
        wrapper.eq("GROUPID", qisRuleCfg.getGroupid())
                .and(qw -> qw
                        .le("LOW_LIMIT", qisRuleCfg.getUpLimit()) // 新下限 <= 旧上限
                        .ge("UP_LIMIT", qisRuleCfg.getLowLimit()) // 新上限 >= 旧下限
                        .or()
                        .le("LOW_LIMIT", qisRuleCfg.getLowLimit()).ge("UP_LIMIT", qisRuleCfg.getUpLimit()) // 完全包含
                );

        // 编辑时排除自身记录
        if (qisRuleCfg.getRecId() != null) {
            wrapper.ne("REC_ID", qisRuleCfg.getRecId());
        }
        return  this.baseMapper.selectCount(wrapper) > 0;
    }
}
